<template>
  <div id="goods_scategory">
    <!--顶部搜索框内容开始-->
    <div class="role_seek">
      <el-input
        class="role_input"
        placeholder="请输入类目ID"
        v-model="forms.id"
        clearable
        @change="seek"
      ></el-input>
      <el-input
        class="role_input"
        placeholder="请输入类目名称"
        v-model="forms.title"
        clearable
        @change="seek"
      ></el-input>
      <el-select v-model="forms.level" placeholder="请选择类目级别" clearable>
        <el-option
          v-for="item in level"
          :key="item.levData"
          :label="item.levName"
          :value="item.levData"
        >
        </el-option>
      </el-select>
      <el-cascader
        placeholder="请选择父类目"
        :options="spulist"
        ref="parent"
        v-model="forms.parentId"
        @change="parentIdsWay"
        :props="{ checkStrictly: true }"
        clearable
      ></el-cascader>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="seek"
       v-power="'operation:category:query'">查找</el-button
      >
      <el-button size="mini" type="primary" icon="el-icon-edit" @click="add"  v-power="'operation:category:create'"
        >添加</el-button
      >
      <!--添加弹出框开始-->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          class="pop-up"
          status-icon
        >
          <el-form-item
            label="类目名称"
            :label-width="formLabelWidth"
            prop="title"
          >
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="类目图片" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="http://192.168.1.99:8080/upload/admin"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
              v-model="form.picUrl"
            >
              <img v-if="form.picUrl" :src="form.picUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item
            label="父类目"
            class="rolelds"
            :label-width="formLabelWidth"
          >
            <el-cascader
              placeholder="选择父类目，默认一级类目"
              :options="spulist"
              ref="dom"
              v-model="form.parentId"
              :props="{ checkStrictly: true }"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="bool ? adduser('form') : compile('form')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!--弹出框结束-->
    </div>
    <!--顶部搜索框内容结束-->

    <!--底部列表内容部分-->
    <template>
      <el-table
        border
        :data="tableData"
        style="width: 100%"
        :default-sort="{ prop: 'date', order: 'descending' }"
      >
        <el-table-column
          prop="value"
          label="类目ID"
          align="center"
        ></el-table-column>
        <el-table-column label="类目名" align="center">
          <template slot-scope="scope">
            <el-tag>
              {{ scope.row.label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="全类目名" align="center" width="210px">
          <template slot-scope="scope">
            <el-tag>
              {{ scope.row.fullName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="父类目ID"
          align="center"
          prop="parent"
        ></el-table-column>
        <el-table-column label="级别" align="center">
          <template slot-scope="scope">
            <el-tag>
              {{ scope.row.level | levelMe }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="标志图片" align="center"> </el-table-column>
        <el-table-column label="类目图片" align="center"
          ><template slot-scope="scope">
            <img :src="scope.row.picUrl"
          /></template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          align="center"
          width="220px"
        >
          <template slot-scope="scope">
            <el-button
              class="button_redact"
              size="mini"
              @click="handleEdit(scope.row)"
              v-power="'operation:category:update'"
              >编辑</el-button
            >
            <el-button
              class="button_remove"
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              v-power="'operation:category:delete'"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--底部列表内容部分结束-->

    <!--底部分页开始-->
    <div class="block">
      <Paging :forms="forms" :gain="gain"></Paging>
    </div>
    <!--底部分页结束-->
  </div>
</template>

<script>
import GoodsCategory from "@/api/goods/GoodsCategory";
import Paging from "../../components/Paging.vue"; //自定义组件

export default {
  data() {
    return {
      //弹出框
      tableData: [{}], //table里面的数据
      dialogFormVisible: false, //控制弹出框是否显示
      formLabelWidth: "120px", //弹出框宽度
      title: "创建", //弹出框标题
      bool: true, //布尔值 判断编辑还是修改 为true是创建 否则就是修改
      input: "", //index框输入内容
      formclone: "",
      //分页
      forms: {
        id: "", //查找的类目id
        title: "", //查找的类目标题
        level: "", //查找的类目等级
        parentId: "", //查找的类目父节点
        limit: 20, //limit
        total: 0, //分页——-共几条ds
        pageNo: 1 //页码
      },
      //类目等级
      level: [
        { levData: 0, levName: "一级类目" },
        { levData: 1, levName: "二级类目" },
        { levData: 2, levName: "三级类目" }
      ],
      //输入框数值
      form: {
        title: "", //类目标题
        parentId: "", //类目父节点
        iconUrl: "", //类目图标
        picUrl: "", //类目图片
        level: "", //类目等级
        id: "" //类目id
      },
      //弹出框验证
      rules: {
        title: [{ message: "类目名不能为空", trigger: "blur", required: true }]
      },
      spulist: [] //递归
    };
  },
  components: {
    Paging
  },
  created() {
    this.gain();
    //spu树接口
    GoodsCategory.spu().then(res => {
      this.spulist = res.data.data;
      this.getTreeData(this.spulist);
    });
    this.formclone = JSON.parse(JSON.stringify(this.form)); //将弹出框初始化状态克隆一份
  },
  filters: {
    //类目
    levelMe(data) {
      if (data == 0) {
        return "一级类目";
      } else if (data == 1) {
        return "二级类目";
      } else if (data == 2) {
        return "三级类目";
      }
    }
  },
  methods: {
    //获取类目id实现查找
    parentIdsWay(e) {
      if (e == null || e === undefined) {
        return false;
      }
      const parent = e[e.length - 1];
      this.forms.parentId = parent;
    },
    //获取列表数据内容
    gain() {
      var _this = this;
      GoodsCategory.gain(_this.forms).then(res => {
        _this.tableData = res.data.data.items; //将接口列表内容赋值给列表数组
        _this.forms.total = res.data.data.total; //分页————条数
        _this.forms.limit = res.data.data.pageSize; //页码长度
        _this.forms.pageNo = res.data.data.pageNo; //页码
      });
    },
    //点击添加按钮触发
    add() {
      this.form = JSON.parse(JSON.stringify(this.formclone)); //将弹出框初始化状态克隆一份
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.title = "创建"; //将标题改变
      this.bool = true; //bool为true触发添加接口
      this.dialogFormVisible = true; //显示弹出框
    },
    //添加类目接口
    adduser(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          if (this.form.parentId) {
            if (this.form.parentId.length > 0) {
              this.form.parentId = this.form.parentId.pop();
            }
          }
          var _this = this;
          GoodsCategory.adduser(_this.form)
            .then(res => {
              if (res.data.errmsg == "成功") {
                this.gain();
                this.Prosperity.Messages("创建成功", "success");
                this.dialogFormVisible = false;
              } else {
                _this.Prosperity.Messages(res.data.errmsg, "error");
              }
            })
            .catch(err => {
              return err;
            });
        } else {
          return false;
        }
      });
    },
    //点击编辑按钮触发
    handleEdit(row) {
      //清空输入框下面的提示（输入错误）
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.form.picUrl = row.picUrl; //图片
      this.form.parentId = row.parent; //类目父节点
      this.form.title = row.label; //类目名
      this.form.id = row.value; //类目ID

      this.bool = false; //改变bool 触发编辑接口
      this.title = "编辑"; //改变弹出框标题
      this.dialogFormVisible = true; //显示弹出框
    },

    //编辑接口
    compile(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          if (this.form.parentId) {
            if (this.form.parentId.length > 0) {
              this.form.parentId = this.form.parentId.pop();
            }
          }
          var _this = this;
          GoodsCategory.compile(_this.form).then(res => {
            if (res.data.errmsg == "成功") {
              this.gain();
              _this.Prosperity.Messages("更新成功", "success");
              _this.dialogFormVisible = false;
            } else {
              _this.Prosperity.Messages(res.data.errmsg, "error");
            }
          });
        } else {
          return false;
        }
      });
    },
    //搜索信息
    seek() {
      this.gain();
    },
    //点击删除按钮
    handleDelete(row) {
      this.form.id = row.value; //当前删除的类目id
      this.$confirm(
        "此操作将永久删除该类目---" + row.fullName + "---, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          //确定删除的话
          this.cancel();
        })
        .catch(() => {
          this.Prosperity.Messages("已取消删除");
        });
    },
    //删除接口
    cancel() {
      var _this = this;
      GoodsCategory.cancel(_this.form)
        .then(res => {
          if (res.data.errmsg == "成功") {
            _this.gain(); //重新渲染
            this.Prosperity.Messages("删除成功", "success");
          } else {
            _this.Prosperity.Messages(res.data.errmsg, "error");
          }
        })
        .catch(err => {
          return err;
        });
    },
    //上传图片
    handleAvatarSuccess(res) {
      this.form.picUrl = res.url;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //递归
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          data[i].children = undefined;
        } else {
          this.getTreeData(data[i].children);
        }
      }
    }
  }
};
</script>

<style scoped="scoped">
/*最大父盒子开始*/
#goods_scategory {
  padding: 20px;
}
/*最大父盒子结束*/

/*顶部index框开始*/
.role_seek > .el-input {
  width: 200px !important;
  height: 40px !important;
  margin-right: 5px;
}
.role_seek > .el-input > .el-input__inner {
  font-size: inherit;
  width: 50% !important;
  height: 100% !important;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}
.role_input .el-input__suffix {
  left: 170px !important;
}
/*顶部index框结束*/

/*顶部按钮开始*/
.role_seek > .el-button--primary {
  line-height: 1px;
  font-size: 14px;
  border: 1px solid #409eff !important;
  background: #409eff !important;
  color: #fff;
  vertical-align: middle;
  padding: 12px 20px;
  margin-left: 5px;
}
.el-cascader {
  margin-left: 5px;
}
/*顶部按钮结束*/

/*底部列表内容*/
img {
  width: 80px;
}
.el-table {
  margin-top: 23px;
  font-size: 12px;
}
.cell {
  text-align: center;
  font-size: 12px;
  padding: 0px 10px;
}
.el-table__row > td {
  padding: 8px 0px;
}
.button_redact {
  background: #409eff !important;
  color: #fff !important;
  border: 1px solid #409eff;
}
/*编辑内容按钮*/
.button_redact:hover {
  background: #66b1ff !important;
  border: 1px solid #66b1ff;
}
.button_remove {
  background: #f56c6c !important;
  color: #fff !important;
  border: #f56c6c 1px solid;
}
.button_remove:hover {
  background: #f78989 !important;
  border: 1px solid #f78989;
}
.el-table--scrollable-x .el-table__body-wrapper {
  overflow-x: hidden;
}
/*底部列表内容*/

/*底部分页*/
.block {
  margin: 55px 0px;
}

/*底部分页结束*/

/*添加弹出框*/
.el-form-item__label {
  text-align: left;
  margin-left: 50px;
  width: 85px !important;
}
.el-input__inner {
  text-indent: 5px;
}
.pop-up
  > .el-form-item
  > .el-form-item__content
  > .el-input
  > .el-input__inner {
  width: 268px !important;
  height: 38px;
  padding: 15px 0px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
}

.pop-up > .el-form-item > .el-form-item__content > .el-form-item__error {
  left: 0px !important;
}
/*头像*/
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 118px;
  height: 118px;
  line-height: 118px;
  text-align: center;
  border: 1px dashed #d9d9d9;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
/*添加弹出框结束*/
.el-select .el-input__inner {
  border: 1px solid #dcdfe6;
}
.el-select__caret {
  position: absolute;
  right: 58px !important;
}
.rolelds .el-input__suffix > i {
  position: absolute;
  right: 24px;
}
</style>
